package me.ywy.opengltest.filter;

import android.content.Context;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import me.ywy.opengltest.utils.OpenGLUtils;

/**
 * Created by ywy on 2018-11-01.
 */
public abstract class AbstractFilter {
    protected final FloatBuffer mGLVertexBuffer;
    protected final FloatBuffer mGLTextureBuffer;
    protected int mVertexShaderId;
    protected int mFragmentShaderId;
    protected int mGLProgramId;
    protected int vPosition;
    protected int vCoord;
    protected int vMatrix;
    protected int vTexture;
    protected int mOutputWidth;
    protected int mOutputHeight;

    /**
     *
     * @param context 上下文
     * @param vertexShaderId  顶点着色器ID
     * @param fragmentShaderId  片元着色器ID
     */
    public AbstractFilter(Context context , int vertexShaderId , int fragmentShaderId) {

        mVertexShaderId = vertexShaderId;
        mFragmentShaderId = fragmentShaderId;


        //创建GL数据缓冲区
        //4个点 每个点两个数据(x,y) 数据类型float
        //顶点坐标
        mGLVertexBuffer = ByteBuffer.allocateDirect(4 * 2 * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        mGLVertexBuffer.clear();
        float[] v = {-1.0f, -1.0f,
                1.0f, -1.0f,
                -1.0f, 1.0f,
                1.0f, 1.0f};
        mGLVertexBuffer.put(v);

        mGLTextureBuffer = ByteBuffer.allocateDirect(4 * 2 * 4).order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        mGLTextureBuffer.clear();
        float[] t = {0.0f, 1.0f,
                1.0f, 1.0f,
                0.0f, 0.0f,
                1.0f, 0.0f};
        //旋转
//        float[] t = {1.0f, 1.0f,
//                1.0f, 0.0f,
//                0.0f, 1.0f,
//                0.0f, 0.0f};
        //镜像
//        float[] t = {1.0f, 0.0f,
//                1.0f, 1.0f,
//                0.0f, 0.0f,
//                0.0f, 1.0f
//        };
        mGLTextureBuffer.put(t);


        initilize(context);
        initCoordinate();
    }


    private void initilize(Context context){
        //根据着色器ID读取自己的着色器字符串
        String vertexSharder = OpenGLUtils.readRawTextFile(context, mVertexShaderId);
        String framentShader = OpenGLUtils.readRawTextFile(context, mFragmentShaderId);
        //产生出已经绑定了着色器的GL程序
        mGLProgramId = OpenGLUtils.loadProgram(vertexSharder, framentShader);

        vPosition = GLES20.glGetAttribLocation(mGLProgramId, "vPosition");
        vCoord = GLES20.glGetAttribLocation(mGLProgramId, "vCoord");
        vMatrix = GLES20.glGetUniformLocation(mGLProgramId, "vMatrix");
        //片元
        vTexture = GLES20.glGetUniformLocation(mGLProgramId, "vTexture");

    }


    protected void initCoordinate(){

    }

    public void onReady(int width, int height) {
        mOutputWidth = width;
        mOutputHeight = height;
    }


    public void release() {
        GLES20.glDeleteProgram(mGLProgramId);
    }

    public int onDrawFrame(int textureId){
        GLES20.glViewport(0,0, mOutputWidth , mOutputHeight);

        //使用着色器程序
        GLES20.glUseProgram(mGLProgramId);
        // 怎么画？ 其实就是传值
        //2：xy两个数据 float的类型
        //1、将顶点数据传入，确定形状
        mGLVertexBuffer.position(0);
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 0, mGLVertexBuffer);
        //传了数据之后 激活
        GLES20.glEnableVertexAttribArray(vPosition);
        //2、将纹理坐标传入，采样坐标
        mGLTextureBuffer.position(0);
        GLES20.glVertexAttribPointer(vCoord, 2, GLES20.GL_FLOAT, false, 0, mGLTextureBuffer);
        GLES20.glEnableVertexAttribArray(vCoord);
        //片元 vTexture 绑定图像数据到采样器
        //激活图层
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        // 图像数据
        // 正常：GLES20.GL_TEXTURE_2D
        // surfaceTexure的纹理需要
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureId);
        //传递参数 0：需要和纹理层GL_TEXTURE0对应
        GLES20.glUniform1i(vTexture,0);
        //参数传完了 通知opengl 画画 从第0点开始 共4个点
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP,0,4);

        return textureId;

        //3、变换矩阵
//        GLES20.glUniformMatrix4fv(vMatrix,1,false,mtx,0);

    }


}
